#ifndef CC772E44_37A6_4A1B_A441_62B06FBBA91B
#define CC772E44_37A6_4A1B_A441_62B06FBBA91B

#include "equation.hpp"
#include "method.hpp"

static double hald_fiv_function(Equation const &eq, double a, double b,
                                double accuracy) {
  double x_1;
  double x_2;
  double y_1;
  double y_2;

  while (true) {
    x_1 = (a + b - accuracy) / 2;
    x_2 = (a + b + accuracy) / 2;

    y_1 = eq.get_equation()(x_1);
    y_2 = eq.get_equation()(x_2);

    if (y_1 > y_2) {
      a = x_1;
    } else {
      b = x_2;
    }

    if (b - a <= 2 * accuracy) {
      return (a + b) / 2;
    }
  }
}

class HalfDivisionMethod
    : public Method<
          std::function<double(Equation const &, double, double, double)>> {
public:
  HalfDivisionMethod()
      : Method(hald_fiv_function, "Метод половинного деления"){};

  double solve(Equation const &eq, double a, double b,
               double accuracy) const override {
    return get_solver()(eq, a, b, accuracy);
  };
};

#endif /* CC772E44_37A6_4A1B_A441_62B06FBBA91B */